﻿using OvenParameterForm.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OvenParameterForm.Technics
{
    public class 保温 : TechnicsBaseClass
    {
        public string 工艺时间 { get; set; }
        private int 当前计时 = 0;
        private int 总时间;
        /// <summary>
        /// 构造函数1，用以将treeNode控件上的信息通过反射的方式转化类，再将类转化为XML，从而最终完成从Treenode转化为XML
        /// </summary>
        /// <param name="strings">数组内容是配方记录的参数</param>
        public 保温(string[] strings):base(strings)
        {
            工艺时间 = strings[1];
        }
        /// <summary>
        /// 构造函数2，通过反射将XML转为类，用以运行工艺
        /// </summary>
        /// <param name="name">烘箱名称，用以查找该烘箱的PLC地址集</param>
        public 保温(string name):base(name) 
        {
        }
        /// <summary>
        /// 保温工艺运行，因为保温工艺有子流程，优先子流程运行，完事再回到保温主流程
        /// </summary>
        public override  void Run()
        {
            当前计时 = 0;
            总时间 = Convert.ToInt32(工艺时间) * 60;
            PLC.WriteData(ovenPLCaddress.烘箱保温地址, "1");
            PLC.WriteData(ovenPLCaddress.加热地址, "1");
            if (子流程.Count > 0)
            {
              Task.Run(() =>
                {
                    子流程.ForEach(f => f.Run());
                });
            } 
            base.Run();
        }
       
        protected override void updatetime(object state)
        {
            当前计时++;
            sendStatus($"正在保温中....{当前计时}/{总时间}");
        }

        protected override bool Complete()
        {
            if (当前计时 >= 总时间)
            {
                PLC.WriteData(ovenPLCaddress.烘箱保温地址, "0");
                PLC.WriteData(ovenPLCaddress.加热地址, "0");
                return true;
            }
            else
            { 
                return false;
            }
           
        }

        protected override void ClearVariable()
        {
            PLC.WriteData(ovenPLCaddress.烘箱保温地址, "0");
            PLC.WriteData(ovenPLCaddress.加热地址, "0");
        }
    }
}
